<?php
//部门
namespace app\crm\controller;

use app\crm\model\Sys;
use think\Config;
use think\Request;
use think\Exception;
use app\crm\model\Department as DepartmentModel;
use app\crm\model\Staff as StaffModel;
use app\crm\model\Cusarea as CusareaModel;
class Cusarea extends \CrmController
{
    public function getarea(){
        try {
            $model=new CusareaModel();
            $data=$model->getallarea();
            $data=$this->tree($data);
            $this->successTerminate($data);
        } catch (Exception $e) {
            $this->serverErrorTerminate($e->getMessage());
        }
    }
     /**
     * 允许限制级数
     * @param $categories
     * @param int $pid
     * @param int $levelLimit
     * @param int $level
     * @return array
     */
    private function tree($categories, $pid = 0, $levelLimit = 0, $level = 1)
    {
        $children = [];
        foreach ($categories as $category) {
            if ($category['parent_id'] === $pid) {

                if ($levelLimit === 0 || $level < $levelLimit) {
                    $category['is_show'] = false;
                    $category['children'] = $this->tree($categories, $category['region_id'], $levelLimit, $level + 1);
                }

                $children[] = $category;
            }
        }
        return $children;
    }
    public function save(Request $request){
        $model=new CusareaModel();
        try {
            $params = delToken($request->get());
            $data["parent_id"]=$params["parent_id"];
            if($data["parent_id"]){
                $data["level"]=$model->getlevel($data["parent_id"]);
            }else{
                $data["level"]=0;
            }
            if($data["level"]>3){
                \exception("销售区域最多只允许添加3级");
            }
            $data["region_name"]=trim($params["region_name"]);
            $has=$model->isrep($data["region_name"]);
            if($has){
                \exception("销售区域已存在");
            }
            $data["region_id"]=$model->add($data);
            $this->successTerminate($data);
        } catch (Exception $e) {
            $this->serverErrorTerminate($e->getMessage());
        }
    }
    public function rename(Request $request){
        $model=new CusareaModel();
        try{
            $params = delToken($request->get());
            $data["region_id"]=$params["region_id"];
            $data["region_name"]=trim($params["region_name"]);
            $has=$model->isrep($data["region_name"]);
            if($has){
                \exception("销售区域已存在");
            }
            $model->rename($data["region_id"],$data["region_name"]);
            $this->successTerminate($data);

        } catch (Exception $e) {
            $this->serverErrorTerminate($e->getMessage());
        }
    }
    public function delete(Request $request){
        $model=new CusareaModel();
        try{
            $params = delToken($request->get());
            $data["region_id"]=$params["region_id"];
           
            $has=$model->haschild($data["region_id"]);
            if($has){
                \exception("销售区域存在下级不能删除");
            }
            $model->deletebyid($data["region_id"]);
            $this->successTerminate($data);

        } catch (Exception $e) {
            $this->serverErrorTerminate($e->getMessage());
        }
    }
}
